<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <div>
      <span>全选:</span>
      <input type="checkbox" v-model="isChecked"/>
      <button @click="fanSelected">反选</button>
      <ul>
        <li  v-for="(item,index) in arr" :key="item.id" >
          <input type="checkbox" v-model="item.checked" />
          <span>{{ item.name }}</span>
        </li>
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          {
            name: "猪八戒",
            checked: false,
          },
          {
            name: "孙悟空",
            checked: false,
          },
          {
            name: "唐僧",
            checked: false,
          },
          {
            name: "白龙马",
            checked: false,
          },
        ],
      },
      computed:{
        isChecked:{
          // 这里是获取看看是不是每一项选中了，如果选中了，那么就会返回一个全选的信号，使得双向绑定的v-model
          // 就会点亮
          get(){
            return this.arr.every(item => item.checked)
          },
          // 这里的set是点击全选会选中
          set(value){
            this.arr.forEach(item => {
              item.checked = value
            });
          } 
        }
      },
      methods:{
        fanSelected(){
          this.isChecked = !this.isChecked
        }
      }
    })
  </script>


</body>

</html>